<template>
  <Dialog v-model="dialogVisible" title="用户导入" width="40%" class="enhanced-dialog">
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:upload" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">用户导入</h4>
        <p class="dialog-subtitle">批量导入用户数据到系统</p>
      </div>
    </div>

    <!-- 上传区域 -->
    <div class="upload-container">
      <el-upload
        ref="uploadRef"
        v-model:file-list="fileList"
        :action="importUrl + '?updateSupport=' + updateSupport"
        :auto-upload="false"
        :disabled="formLoading"
        :headers="uploadHeaders"
        :limit="1"
        :on-error="submitFormError"
        :on-exceed="handleExceed"
        :on-success="submitFormSuccess"
        accept=".xlsx, .xls"
        drag
        class="enhanced-upload"
      >
        <div class="upload-content">
          <div class="upload-icon">
            <Icon icon="ep:upload" />
          </div>
          <div class="upload-text">
            <p class="main-text">将文件拖到此处，或<em class="click-text">点击上传</em></p>
            <p class="sub-text">支持 Excel 文件格式</p>
          </div>
        </div>

        <template #tip>
          <div class="upload-tips">
            <!-- 更新选项 -->
            <div class="update-option">
              <el-checkbox v-model="updateSupport" class="enhanced-checkbox" />
              <span class="option-text">是否更新已经存在的用户数据</span>
            </div>

            <!-- 文件格式提示 -->
            <div class="format-tip">
              <Icon icon="ep:info" class="tip-icon" />
              <span class="tip-text">仅允许导入 xls、xlsx 格式文件</span>
            </div>

            <!-- 下载模板 -->
            <div class="template-download">
              <el-link
                :underline="false"
                type="primary"
                @click="importTemplate"
                class="download-link"
              >
                <Icon icon="ep:download" class="link-icon" />
                下载模板
              </el-link>
            </div>
          </div>
        </template>
      </el-upload>
    </div>

    <!-- 弹窗底部 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button
          :disabled="formLoading"
          type="primary"
          @click="submitForm"
          class="submit-btn"
          :loading="formLoading"
        >
          <Icon icon="ep:upload" class="btn-icon" v-if="!formLoading" />
          {{ formLoading ? '导入中...' : '确 定' }}
        </el-button>
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取 消
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import * as UserApi from '@/api/system/user'
import { getAccessToken, getTenantId } from '@/utils/auth'
import download from '@/utils/download'

defineOptions({ name: 'SystemUserImportForm' })

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中
const uploadRef = ref()
const importUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
const uploadHeaders = ref() // 上传 Header 头
const fileList = ref([]) // 文件列表
const updateSupport = ref(0) // 是否更新已经存在的用户数据

/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  fileList.value = []
  resetForm()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const submitForm = async () => {
  if (fileList.value.length == 0) {
    message.error('请上传文件')
    return
  }
  // 提交请求
  uploadHeaders.value = {
    Authorization: 'Bearer ' + getAccessToken(),
    'tenant-id': getTenantId()
  }
  formLoading.value = true
  uploadRef.value!.submit()
}

/** 文件上传成功 */
const emits = defineEmits(['success'])
const submitFormSuccess = (response: any) => {
  if (response.code !== 0) {
    message.error(response.msg)
    formLoading.value = false
    return
  }
  // 拼接提示语
  const data = response.data
  let text = '上传成功数量：' + data.createUsernames.length + ';'
  for (let username of data.createUsernames) {
    text += '< ' + username + ' >'
  }
  text += '更新成功数量：' + data.updateUsernames.length + ';'
  for (const username of data.updateUsernames) {
    text += '< ' + username + ' >'
  }
  text += '更新失败数量：' + Object.keys(data.failureUsernames).length + ';'
  for (const username in data.failureUsernames) {
    text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
  }
  message.alert(text)
  // 发送操作成功的事件
  emits('success')
}

/** 上传错误提示 */
const submitFormError = (): void => {
  message.error('上传失败，请您重新上传！')
  formLoading.value = false
}

/** 重置表单 */
const resetForm = () => {
  // 重置上传状态和文件
  formLoading.value = false
  uploadRef.value?.clearFiles()
}

/** 文件数超出提示 */
const handleExceed = (): void => {
  message.error('最多只能上传一个文件！')
}

/** 下载模板操作 */
const importTemplate = async () => {
  const res = await UserApi.importUserTemplate()
  download.excel(res, '用户导入模版.xlsx')
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 上传容器 */
.upload-container {
  padding: 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-upload {
  :deep(.el-upload) {
    width: 100%;
  }

  :deep(.el-upload-dragger) {
    width: 100%;
    height: auto;
    min-height: 180px;
    border: 2px dashed $primary-border;
    border-radius: $border-radius;
    background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
    transition: all 0.3s ease;
    padding: 24px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    &:hover {
      border-color: $primary-color;
      background: $primary-light;
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(0, 82, 204, 0.15);
    }

    @media (max-width: 768px) {
      min-height: 150px;
      padding: 20px;
    }
  }

  :deep(.el-upload-dragger.is-dragover) {
    border-color: $primary-color;
    background: rgba(0, 82, 204, 0.05);
  }
}

.upload-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;

  @media (max-width: 768px) {
    gap: 12px;
  }
}

.upload-icon {
  width: 60px;
  height: 60px;
  background: $primary-light;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: $primary-color;
  transition: all 0.3s ease;

  @media (max-width: 768px) {
    width: 50px;
    height: 50px;
    font-size: 20px;
  }
}

.upload-text {
  text-align: center;

  .main-text {
    font-size: 16px;
    color: $text-regular;
    margin: 0 0 8px 0;
    font-weight: 500;

    @media (max-width: 768px) {
      font-size: 14px;
    }

    .click-text {
      color: $primary-color;
      font-style: normal;
      font-weight: 600;
      text-decoration: underline;
    }
  }

  .sub-text {
    font-size: 13px;
    color: $text-secondary;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

/* 上传提示区域 */
.upload-tips {
  margin-top: 20px;
  padding: 16px;
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  border-radius: $border-radius-small;
  border: 1px solid rgba(0, 82, 204, 0.1);

  @media (max-width: 768px) {
    margin-top: 16px;
    padding: 12px;
  }
}

.update-option {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;

  @media (max-width: 768px) {
    margin-bottom: 10px;
  }
}

.enhanced-checkbox {
  :deep(.el-checkbox__input) {
    .el-checkbox__inner {
      border-color: $primary-border;

      &:hover {
        border-color: $primary-color;
      }
    }

    &.is-checked {
      .el-checkbox__inner {
        background-color: $primary-color;
        border-color: $primary-color;
      }
    }
  }
}

.option-text {
  font-size: 14px;
  color: $text-regular;
  font-weight: 500;

  @media (max-width: 768px) {
    font-size: 13px;
  }
}

.format-tip {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;

  @media (max-width: 768px) {
    margin-bottom: 10px;
  }
}

.tip-icon {
  font-size: 14px;
  color: $primary-color;
  flex-shrink: 0;
}

.tip-text {
  font-size: 12px;
  color: $text-secondary;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.template-download {
  display: flex;
  justify-content: center;
}

.download-link {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: $border-radius-small;
  background: rgba(0, 82, 204, 0.05);
  border: 1px solid $primary-border;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-light;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
  }

  @media (max-width: 768px) {
    font-size: 12px;
    padding: 6px 12px;
  }
}

.link-icon {
  font-size: 14px;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
  }
}

.submit-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 120px;
  justify-content: center;

  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 12px 24px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }

  .btn-icon {
    font-size: 14px;
  }
}

/* 上传文件列表样式 */
:deep(.el-upload-list) {
  margin-top: 16px;

  .el-upload-list__item {
    border-radius: $border-radius-small;
    border: 1px solid $border-light;
    background: white;
    transition: all 0.3s ease;

    &:hover {
      border-color: $primary-color;
      box-shadow: 0 2px 8px rgba(0, 82, 204, 0.1);
    }

    .el-upload-list__item-name {
      color: $text-regular;
      font-weight: 500;
    }

    .el-upload-list__item-status-label {
      color: $success-color;
    }
  }

  .el-upload-list__item.is-success {
    border-color: rgba(103, 194, 58, 0.3);
    background: rgba(103, 194, 58, 0.05);
  }

  .el-upload-list__item.is-uploading {
    border-color: $primary-border;
    background: $primary-light;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .upload-container {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .enhanced-upload {
    :deep(.el-upload-dragger) {
      background: linear-gradient(135deg, #4b5563 0%, #374151 100%);
      border-color: rgba(96, 165, 250, 0.3);

      &:hover {
        background: rgba(96, 165, 250, 0.1);
        border-color: #60a5fa;
      }
    }
  }

  .upload-icon {
    background: rgba(96, 165, 250, 0.1);
    color: #60a5fa;
  }

  .upload-text {
    .main-text {
      color: #f9fafb;

      .click-text {
        color: #60a5fa;
      }
    }

    .sub-text {
      color: #9ca3af;
    }
  }

  .upload-tips {
    background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
    border-color: rgba(96, 165, 250, 0.3);
  }

  .option-text {
    color: #f9fafb;
  }

  .tip-text {
    color: #d1d5db;
  }

  .tip-icon {
    color: #60a5fa;
  }

  .download-link {
    background: rgba(96, 165, 250, 0.1);
    border-color: rgba(96, 165, 250, 0.3);
    color: #60a5fa;

    &:hover {
      background: rgba(96, 165, 250, 0.2);
    }
  }

  :deep(.el-upload-list) {
    .el-upload-list__item {
      background: #4b5563;
      border-color: #6b7280;

      &:hover {
        border-color: #60a5fa;
      }

      .el-upload-list__item-name {
        color: #f9fafb;
      }

      .el-upload-list__item-status-label {
        color: #34d399;
      }
    }

    .el-upload-list__item.is-success {
      border-color: rgba(52, 211, 153, 0.3);
      background: rgba(52, 211, 153, 0.05);
    }

    .el-upload-list__item.is-uploading {
      border-color: rgba(96, 165, 250, 0.3);
      background: rgba(96, 165, 250, 0.1);
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .upload-container {
    padding: 12px;
  }

  .enhanced-upload {
    :deep(.el-upload-dragger) {
      min-height: 120px;
      padding: 16px;
    }
  }

  .upload-icon {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .upload-text {
    .main-text {
      font-size: 13px;
    }

    .sub-text {
      font-size: 11px;
    }
  }

  .upload-tips {
    padding: 10px;
  }

  .option-text {
    font-size: 12px;
  }

  .tip-text {
    font-size: 10px;
  }

  .download-link {
    font-size: 11px;
    padding: 5px 10px;
  }

  .dialog-footer {
    padding: 12px 16px;
    gap: 8px;
  }
}
</style>
